package exceltocard;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.text.Text;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class ViewController extends Application implements Initializable{
	public Stage primaryStage;
	public File excelFile ;
	public File vcardFile ;
	@FXML
	public BorderPane root;
	@FXML
	public GridPane gpane;
	@FXML
	public Label uploadExcelLabel;
	@FXML
	public Label saveVcardLabel;
	@FXML
	public Text successmsg;
	@FXML
	public Text errormsg;
	public FileChooser fileChooser = null;
	@FXML
	public TextField nameCell;
	@FXML
	public TextField numberCell;
	public static void main(String[] args) {
		Application.launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		// TODO Auto-generated method stub
		this.primaryStage = stage;
//		FXMLLoader loader = new FXMLLoader(Splash.ViewController.class.getResource("View.fxml"));

		primaryStage.getIcons().add(new Image("/exceltocard/sss.jpg"));
		// Path to the FXML File
//		final File file = new File(ViewController.class.getProtectionDomain().getCodeSource().getLocation().getPath());
//		String fxmlDocPath = file.getAbsolutePath().toString()+"/exceltocard/View.fxml";
//		String fxmlDocPath = ViewController.class.getResource("View.fxml").getPath().replaceFirst("/","");
//		FileInputStream fxmlStream = new FileInputStream(fxmlDocPath);
		// Create the Pane and all Details
		FXMLLoader loader = new FXMLLoader(ViewController.class.getResource("View.fxml"));
		//root = (BorderPane) loader.load(fxmlStream);
		root = (BorderPane) loader.load();
		// Create the Scene
		Scene scene = new Scene(root);
		// Set the Scene to the Stage
		fileChooser = new FileChooser();
		fileChooser.setTitle("打开Excel文件");
		stage.setScene(scene);
		// Set the Title to the Stage
		stage.setTitle("Excel 转 Vcard --- Vcard 转 Excel");
		// Display the Stage
		stage.show();
	}

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
	}
	@FXML
	public void addFile() {
		// TODO Auto-generated method stub
		FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("xlscsv","*.xlsx","*.xls","*.csv","*.vcf");
        fileChooser.getExtensionFilters().add(extFilter);
        excelFile = fileChooser.showOpenDialog(primaryStage);
		String filename = excelFile.getName();
        uploadExcelLabel.setText( filename+"加载完成。");
	}
	@FXML
	public void selectVcard(){
		FileChooser fileChooser = new FileChooser();
		  
        //Set extension filter
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("VCARD files", "*.vcf");
		FileChooser.ExtensionFilter extFilter1 = new FileChooser.ExtensionFilter("XLSX files", "*.xlsx");
		FileChooser.ExtensionFilter extFilter2 = new FileChooser.ExtensionFilter("XLS files", "*.xls");
		fileChooser.getExtensionFilters().add(extFilter);
		fileChooser.getExtensionFilters().add(extFilter1);
		fileChooser.getExtensionFilters().add(extFilter2);

        //Show save file dialog
        vcardFile = fileChooser.showSaveDialog(primaryStage);
        if(vcardFile != null){
            SaveFile(vcardFile);
        }
		String vcardname = vcardFile.getName();
        saveVcardLabel.setText(vcardname+"注意：选好导出格式，再点开始转换。");
	}
	private void SaveFile(File file){
        try {
            FileWriter fileWriter = null;
            fileWriter = new FileWriter(file);
            fileWriter.close();
        } catch (IOException ex) {
           ex.printStackTrace();
        }
    }
	@FXML
	public void startConverting(){
		String nacell =nameCell.getText();
		String parint =numberCell.getText();
		if("".equals(nacell)||"".equals(parint)) {
			nacell="1";
			parint="2";
		}
		Integer namecell = Integer.parseInt(nacell);
		Integer parseint = Integer.parseInt(parint);

		try{
		if(Maincard.exceltocard(excelFile, vcardFile,namecell , parseint)){
			successmsg.setText("转换成功");
			errormsg.setText(null);
		}
		else{
			successmsg.setText(null);
			errormsg.setText("转换出错，请查看使用说明！");
		}
		}
		catch (Exception e) {
			// TODO: handle exception
			successmsg.setText(null);
			errormsg.setText("转换出错，请联系作者！");
			e.printStackTrace();
		}
	}
}
